package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerTraceHistoryQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//类似之前的Servlet
//请求用户的请求和相应

@Controller//IOC注解，将Controller交给Spring容器管理
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {

    //自动去Spring容器中找到对应类型或者名称的对象并设置给当前字段
    @Autowired
    private ICustomerTraceHistoryService service;
    @Autowired
    private IRoleService roleService ;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService ;

    //处理查询的请求
    //查询潜在客户
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerTraceHistoryQueryObject qo, HttpSession session) {
        //查询到所有的部门
        PageResult result = service.query(qo);
        //将查询到的部门共享给页面
        model.addAttribute("result",result);


        //跟进方式
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.listByDicSn("communicationMethod");
        model.addAttribute("communicationMethods", communicationMethods);


        //指定页面跳转的视图
        return "customerTraceHistory/list";
    }


    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            CustomerTraceHistory dept = service.get(id);
            model.addAttribute("entity", dept);
        }
        //跳转到input.jsp页面
        return "customerTraceHistory/input";
    }

    //执行保存或更新
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(CustomerTraceHistory c, HttpSession session) {
        if (c.getId() != null) {
            //更新
            service.update(c);
        } else {

            Employee employee = (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");
            c.setInputUser(employee);

            c.setInputTime(new Date());

            service.save(c);

        }

        return "redirect:/customerTraceHistory/list.do";
    }

}
